//
//  HRLeftMenuView.m
//  Hear
//
//  Created by mac on 2017/3/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRLeftMenuView.h"
#import "UIView+YYAdd.h"
#import "HRBaseTableView.h"
#import "HRLeftMenuCell.h"
#import "HRLeftMenuHeadView.h"
#import "UIImage+BlurEffects.h"
#import "HRCoverImageModel.h"
#import "HRShowGuideView.h"
@interface HRLeftMenuView ()<UIGestureRecognizerDelegate, UIActionSheetDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)HRBaseTableView *tableView;
@property (nonatomic, strong)NSArray *menuListArr;
@property (nonatomic, strong)NSArray *menuIconArr;
@property (nonatomic, strong)HRLeftMenuHeadView *menuHeadView;
@property (nonatomic, strong) UIViewController *ContainerVC;
@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong)UIImageView *backImageView;

@end
@implementation HRLeftMenuView
static HRLeftMenuView *menuView = nil;


-(HRLeftMenuHeadView *)menuHeadView
{
    if (_menuHeadView == nil)
    {
        
        HRLeftMenuHeadView *menuHeadView = [[HRLeftMenuHeadView alloc] initWithFrame:CGRectMake(0, 0, self.width, 180)];
        _menuHeadView = menuHeadView;
        WeakSelf
        _menuHeadView.chageUserInfoBlock = ^(){
            if (weakSelf.chageUserInfoBlock){
                weakSelf.chageUserInfoBlock();
            }
        };
    }
    return _menuHeadView;
}

-(HRBaseTableView *)tableView
{
    if (_tableView == nil)
    {
        HRBaseTableView *tableView = [HRBaseTableView new];
//        tableView.backgroundColor = CustomColor(240, 240, 240, 1);
        tableView.backgroundColor = [MainColor colorWithAlphaComponent:0.95];

        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.rowHeight = CGHeightMT(50);
        [self addSubview:(self.tableView = tableView)];
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.mas_equalTo(0);
        }];
        
    }
    return _tableView;
}


+ (instancetype)ShareManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        menuView = [[self alloc] initWithFrame:CGRectMake(-menuViewWith, 0, menuViewWith, kFullHeight)];
    });
    return menuView;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        menuView = [super allocWithZone:zone];
    });
    return menuView;
}
- (instancetype)initWithContainerViewController:(UIViewController *)containerVC{
    if (self = [super init]) {
        _ContainerVC = containerVC;
        self.isLeftViewHidden = YES;
        _maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//        _maskView.backgroundColor = CustomColor(240, 240, 240, 1);
        _maskView.backgroundColor = [NavTinColor colorWithAlphaComponent:0.95];
        _maskView.hidden = YES;
        [_ContainerVC.view addSubview:_maskView];
        [self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
        [self addRecognizer];
        
    }
    
    return self;
}


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.menuListArr = @[@"Hear",@"点滴",@"我的主页",@"我的关注",@"设置",@"新手引导",@"推荐好友"];
        self.menuIconArr = @[@"menu_Hear",@"侧滑页-点滴",@"我的主页",@"我的关注",@"设置",@"问号圆圈",@"推荐"];
        

//        UIImageView *backImageView = [UIImageView new];
//        CGFloat blurRadius = 20.f;
//        CGFloat saturationDeltaFactor = 1.0;
//        backImageView.image = [UIImage ty_imageByApplyingBlurToImage:[UIImage imageNamed:ICONNAME] withRadius:blurRadius tintColor:[[UIColor colorWithHexString:ICONCOLOR] colorWithAlphaComponent:0.5] saturationDeltaFactor:saturationDeltaFactor maskImage:nil];
//        [self addSubview:backImageView];
//        self.backImageView = backImageView;
        
//        [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.top.bottom.mas_equalTo(0);
//        }];
        
        [self addSubview:self.tableView];
        self.tableView.tableHeaderView = self.menuHeadView;
        
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadNotifi) name:@"reloadIconAndBackGroundNotififation" object:nil];
    }
    return self;
}

- (void)reloadNotifi{
    CGFloat blurRadius = 20.f;
    CGFloat saturationDeltaFactor = 1.0;
    WeakSelf
    self.backImageView.image = [UIImage ty_imageByApplyingBlurToImage:[UIImage imageNamed:ICONNAME] withRadius:blurRadius tintColor:[[UIColor colorWithHexString:ICONCOLOR] colorWithAlphaComponent:0.5] saturationDeltaFactor:saturationDeltaFactor maskImage:nil];
    
    self.menuHeadView.nickNameLabel.text = NICKNAME;
    
//    self.menuHeadView.iconImageView.image = [HRCoverImageModel OriginImage:[UIImage imageNamed:ICONNAME] scaleToSize:CGSizeMake(80, 80)];
    
    [HRCoverImageModel OriginImage:[UIImage imageNamed:ICONNAME] scaleToSize:CGSizeMake(80, 80) callBackOriginImage:^(UIImage *originImage) {
        weakSelf.menuHeadView.iconImageView.image = originImage;
    }];
    
    
    self.menuHeadView.iconImageView.backgroundColor = [UIColor colorWithHexString:ICONCOLOR];
    
    
}







-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"frame"]) {
        CGRect new = [change[@"new"] CGRectValue];
        CGFloat x = new.origin.x;
        if (x != - menuViewWith) {
            _maskView.hidden = NO;
            _maskView.alpha = (x + menuViewWith)/menuViewWith*0.5;
        }else
        {
            _maskView.hidden = YES;
        }
    }
}

#pragma mark - UIPanGestureRecognizer
-(void)addRecognizer{
    
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(didPanEvent:)];
    pan.delegate = self;
    [_ContainerVC.view addGestureRecognizer:pan];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeLeftViewEvent:)];
    [_maskView addGestureRecognizer:tap];
    
}

-(void)closeLeftViewEvent:(UITapGestureRecognizer *)recognizer{
    
    [self closeLeftView];
}


-(void)didPanEvent:(UIPanGestureRecognizer *)recognizer{
    
    CGPoint translation = [recognizer translationInView:_ContainerVC.view];
    //NSLog(@"translation.x == %f", translation.x);
    [recognizer setTranslation:CGPointZero inView:_ContainerVC.view];
    
    if(UIGestureRecognizerStateBegan == recognizer.state ||
       UIGestureRecognizerStateChanged == recognizer.state){
        
        if (translation.x > 0 ) {//SwipRight
            
            if (self.origin.x == 0) {
                return;
            }
            CGFloat tempX = self.origin.x + translation.x;
            if (tempX <= 0) {
                
                self.frame = CGRectMake(tempX, 0, menuViewWith, kFullHeight);
                
            }else{
                
                self.frame = CGRectMake(0, 0, menuViewWith, kFullHeight);
            }
            
        }else{//SwipLeft
            
            CGFloat tempX = self.origin.x + translation.x;
            self.frame = CGRectMake(tempX, 0, menuViewWith, kFullHeight);
        }
        
    }else{
        
        if (self.origin.x >= - menuViewWith * 0.5) {
            
            [self openLeftView];
            
        }else{
            
            [self closeLeftView];
        }
    }
}

/**
 *  关闭左视图
 */
- (void)closeLeftView{
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.frame = CGRectMake(- menuViewWith, 0, menuViewWith, kFullHeight);
        self.isLeftViewHidden = YES;
        _maskView.hidden = YES;
    }];
    
}

- (void)openLeftView{
    
    
    
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.frame = CGRectMake(0, 0, menuViewWith, kFullHeight);
        self.isLeftViewHidden = NO;
    } completion:^(BOOL finished) {
        
        _maskView.hidden = NO;
        _maskView.alpha = 0.5;
        
        if (![[NSUserDefaults standardUserDefaults]boolForKey:@"头像提示页面"]){
//            [HRShowGuideView showGuideViewImageName:@"头像提示页面" andGuideFrame:CGRectMake((kFullWidth - 240)/2, 130, 240, 140)];
        }

    }];
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.menuListArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HRLeftMenuCell *cell = [HRLeftMenuCell cellWithTableView:tableView];
    if (indexPath.row != 0){
        cell.switchView.hidden = YES;
        cell.weomanLabel.hidden = YES;
        cell.manLabel.hidden = YES;
    }else{
        cell.switchView.hidden = NO;
        cell.weomanLabel.hidden = NO;
        cell.manLabel.hidden = NO;
    }
    cell.iconImageView.image = [UIImage imageNamed:self.menuIconArr[indexPath.row]];
    cell.descLabel.text = self.menuListArr[indexPath.row];
    WeakSelf
    cell.isButtonOnBlock = ^(BOOL isOn){
        if (weakSelf.isButtonOnBlock){
            weakSelf.isButtonOnBlock(isOn);
        }
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self closeLeftView];

    if (self.menuViewDelegate)
    {
        [self.self.menuViewDelegate LeftMenuViewActionIndex:indexPath.row];
    }
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self removeObserver:self forKeyPath:@"frame"];
}

@end
